package org.rjxh.examfront.comtroller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.rjxh.examfront.pojo.*;
import org.rjxh.examfront.service.IExamExaminationService;
import org.rjxh.examfront.service.IExamQuestionService;
import org.rjxh.examfront.service.IExamRecordService;
import org.rjxh.examfront.service.IExamRespondService;
import org.rjxh.examfront.web.tools.RedisCache;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/exam")
public class ExamController {
    @Resource
    private IExamExaminationService examExaminationService;

    @Resource
    private IExamQuestionService examQuestionService;

    @Resource
    private IExamRespondService examRespondService;
    @Resource
    private IExamRecordService examRecordService;

    @Resource
    private RedisCache redisCache;

    /**
     *  获取当前登录用户的考试列表
     * @return
     */
    @GetMapping("/list")
    public R getExaminationList() {
        return examExaminationService.getExaminationListByUserID();
    };


    /**
     * 开始考试请求
     * @param examID
     * @return
     */
    @PostMapping("/startExam")
    public R startExam(@RequestParam Long examID) {
        return examExaminationService.startExam(examID);
    }


    /**
     * 获取考试题目
     * @param page
     * @return
     */
    @GetMapping("/getExamQuestions")
    public R getExamQuestions(@RequestBody(required = false) PageParam page) {
        if (Objects.isNull(page)){
            //临时不分页
            return examQuestionService.getQuestions(new Page<ExamQuestion>().setSize(-1));
        }
        return examQuestionService.getQuestions(new Page<>(page.getCurrentPage(), page.getPageSize()));
    }

    @PostMapping("/updateResponds")
    public R updateResponds(@RequestBody List<ExamRespond> examResponds) {
        //examResponds.forEach(examRespond -> {
        //    System.out.println(examRespond);
        //});
        if (Objects.isNull(examResponds)){
            return new R(400, "参数错误");
        }
        return examRespondService.updateResponds(examResponds);

    }

    @PostMapping("finishExam")
    public R finishExam() {
        return examRecordService.finishExam();
    }
}
